package main.file;

import org.junit.Test;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;


/**
 * Created by Administrator on 2017/5/9.
 */
public class copyTest {
    @Test
    public void test2()  throws IOException{
        File a = new File("d:\\testa");
        File b = new File("d:\\testb");
        String[] strArr = a.list(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return new File(dir,name).isFile()&&name.endsWith(".log");
            }
        });

        for(String str :strArr){
            BufferedReader br = new BufferedReader(new FileReader(new File(a,str)));
            BufferedWriter bw = new BufferedWriter(new FileWriter(new File(b,str.replace(".log",".txt"))));
            String line = null;
            while ((line=br.readLine())!=null){
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
            br.close();
            bw.close();
        }
    }


    //jdk1.7文件操作新特性使用
    @Test
    public void test() throws IOException {
        String a = "d:\\testa";
        String b = a+"\\1.log";
        String content = "这是一个测试a";
        //创建文件
        File temp = new File(a);
        temp.mkdirs();
        //写文件
        Files.write(Paths.get(b),content.getBytes());
        //取出文件并读出内容
        String cont = new String( Files.readAllBytes(Paths.get(b)));
        System.out.println(cont);
    }

}
